import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/routes/call/controller/xindong_call_controller.dart';

// 通话
class XinDongCallHandle {
  // 检测
  // {"user_id":5555824,"another_id":"5559798","channel":"channel5559798","types":1,"orderid":"41612"}
  final String checkCall = "/api/member/checkMic";

  // 呼叫 types  0取消 | 1接听 | 2无权限
  // {"user_id":5555824,"another_id":"5559798","channel":"channel5559798","types":1,"orderid":"41612"}
  final String liveCall = "/apis/index.php/home/Privatechat/toRequest";

  // 接听 拒绝
  //{"user_id":"5555824","another_id":"5559798","up_status":"5559798","channel":"channel5559798","status":0,"types":2,"orderid":"55779"}
  final String liveAnswer = "/apis/index.php/home/Privatechat/return_call_request";

  // 取消通话
  //{"user_id":"5559798","orderid":"41698","another_id":"9999"}
  final String callCancel = "/apis/index.php/home/Privatechat/toCancelCall";

  // api/member/che_order_max_duration
  // 参数：order_id=2（发起视频的时候返回）、max_duration=40（发起视频的时候，单位：秒）、user_id=5559798（当前登录人的ID）
  final String callTime = "/api/member/che_order_max_duration";

  // 检测能不能呼叫
  Future<bool> xindong_checkCall({required dynamic userId, required int callType}) async {
    if (!XinDongUserController.to.isUserCompleted) {
      return false;
    }
    final object = await XinDongNetworking.instance.request(
      checkCall,
      method: XinDongHttpMethod.post,
      parameters: {
        "types": callType,
        "to_user": userId,
        "channel": "channel$userId",
        "isapp": 1,
      },
    );
    // 成功和余额不足都可以呼叫
    if (object.code == XinDongServerStatus.success || object.code == XinDongServerStatus.poor) {
      XinDongCallController.to.currentOrderId = object.data["orderid"];
      XinDongCallController.to.currentPrice = object.data["price"] ?? "0";
      return true;
    }
    return false;
  }

  // 检测还能通话多长时间，发起人请求
  Future<bool> xindong_checkTimeThanStop(int duration) async {
    if (!XinDongUserController.to.isUserCompleted) {
      return false;
    }
    final object = await XinDongNetworking.instance.request(
      callTime,
      method: XinDongHttpMethod.post,
      parameters: {
        "order_id": XinDongCallController.to.currentOrderId,
        "max_duration": duration,
      },
    );
    return object.code == XinDongServerStatus.poor;
  }

  // 取消
  Future<bool> xindong_callCancel() async {
    if (!XinDongUserController.to.isUserCompleted) {
      return false;
    }
    final object = await XinDongNetworking.instance.request(
      callCancel,
      method: XinDongHttpMethod.post,
      parameters: {
        "another_id": XinDongCallController.to.targetUser.uid,
        "orderid": XinDongCallController.to.currentOrderId,
      },
    );
    return object.code == XinDongServerStatus.success;
  }

  // 呼叫，服务端发信令
  // Future<bool> callWithType({required int callType}) async {
  //   if (!XinDongUserController.to.isUserCompleted) {
  //     return false;
  //   }
  //   final object = await XinDongNetworking.instance.request(
  //     liveCall,
  //     method: XinDongHttpMethod.post,
  //     parameters: {
  //       "types": callType,
  //       "another_id": XinDongCallController.to.targetUser.uid,
  //       "orderid": XinDongCallController.to.currentOrderId,
  //       "channel": "channel${XinDongCallController.to.targetUser.uid}",
  //     },
  //   );
  //   // 成功和余额不足都可以呼叫
  //   if (object.code == XinDongServerStatus.success || object.code == XinDongServerStatus.poor) {
  //     return true;
  //   }
  //   return false;
  // }

  // 操作
  // 0取消 | 1接听 | 2无权限 XinDongActionType
  Future<bool> answerWithStatus({required int status}) async {
    if (!XinDongUserController.to.isUserCompleted) {
      return false;
    }
    final object = await XinDongNetworking.instance.request(
      liveAnswer,
      method: XinDongHttpMethod.post,
      parameters: {
        "types": XinDongCallController.to.currentCallType,
        "another_id": XinDongCallController.to.targetUser.uid,
        "orderid": XinDongCallController.to.currentOrderId,
        "status": status,
      },
    );
    // 成功和余额不足都可以呼叫
    if (object.code == XinDongServerStatus.success || object.code == XinDongServerStatus.poor) {
      return true;
    }
    return false;
  }
//
// // 取消
// Future<bool> taoke_cancel(int liveRecordId, {bool isTimeout = false}) async {
//   final object = await XinDongNetworking.instance.request(
//     liveCancel,
//     method: XinDongHttpMethod.post,
//     parameters: {"liveRecordId": liveRecordId, "isTimeout": isTimeout ? 1 : 0},
//   );
//   return object.code == XinDongServerStatus.success;
// }
//
// // 拒绝
// Future<bool> taoke_refuse(int liveRecordId) async {
//   final object = await XinDongNetworking.instance.request(
//     liveRefuse,
//     method: XinDongHttpMethod.post,
//     parameters: {"liveRecordId": liveRecordId},
//   );
//   return object.code == XinDongServerStatus.success;
// }
//
// // 结束
// Future<bool> taoke_finish(int liveRecordId) async {
//   final object = await XinDongNetworking.instance.request(
//     liveFinish,
//     method: XinDongHttpMethod.post,
//     parameters: {"liveRecordId": liveRecordId},
//   );
//   return object.code == XinDongServerStatus.success;
// }
//
// // 接受
// Future<XinDongCallServerClass?> taoke_accept(int liveRecordId) async {
//   final object = await XinDongNetworking.instance.request(
//     liveAccept,
//     method: XinDongHttpMethod.post,
//     parameters: {"liveRecordId": liveRecordId},
//   );
//   if (object.code == XinDongServerStatus.success) {
//     // uid roomId amount nextChargeTime
//     return XinDongCallServerClass.fromJson(object.data);
//   }
//   return null;
// }
//
// // 消费 定时扣费
// Future<XinDongCallServerClass?> taoke_consume() async {
//   final object = await XinDongNetworking.instance.request(
//     liveConsume,
//     method: XinDongHttpMethod.post,
//     parameters: {"liveRecordId": XinDongCallController.to.serverClass.liveRecordId},
//   );
//   if (object.code == XinDongServerStatus.success) {
//     // roomId: 10135, uid: 10123, amount: 421250, price: 5, nextChargeTime: 1653704451000, sysCurrentTime: 1653704392184, useFreeLive: null, liveRecordId: null, sysNotice: null
//     return XinDongCallServerClass.fromJson(object.data);
//   } else if (object.code == XinDongServerStatus.poor) {}
//   return null;
// }

// // 检验主播是否能呼叫我
// // 未登录，部分用户不开启
// Future<bool> taoke_liveFetch(String userId) async {
//   if (!XinDongUserController.to.isUserCompleted) {
//     return false;
//   }
//   final object = await XinDongNetworking.instance.request(
//     liveFetch,
//     method: XinDongHttpMethod.post,
//     parameters: {"uid": userId, "ver": 1},
//   );
//   if (object.code == XinDongServerStatus.success) {
//     // 0 能 1 不能
//     return object.data == 0;
//   }
//   return false;
// }
//
// 自动呼叫
// 未登录，部分用户不开启
// return 0 继续跑定时器，1停止跑定时器
// Future<bool> taoke_liveAutoCall() async {
//   if (!XinDongUserController.to.isUserCompleted) {
//     return false;
//   }
//   final object = await XinDongNetworking.instance.request(
//     liveAutoCall,
//     method: XinDongHttpMethod.post,
//   );
//   if (object.code == XinDongServerStatus.success) {
//     return object.data == 0;
//   }
//   return false;
// }
}
